/**
 * Project: ctu-module
 * 
 * File Created at 2011-2-16
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.mvel;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * TODO Comment of UserInfo
 * 
 * @author yeqiang.dengyq
 */
public class UserInfo {

    private String customerID;
    private Map<String, Object> dataModelMap = new HashMap<String, Object>();
    private Map<String, Object> tempValueMap = new HashMap<String, Object>();
    private Map<String, Object> outputValueMap = new HashMap<String, Object>();

    public UserInfo(String customerID, Map<String, Object> dataModelMap, Map<String, Object> tempValueMap,
                    Map<String, Object> outputValueMap) {
        this.customerID = customerID;
        this.dataModelMap = dataModelMap;
        this.tempValueMap = tempValueMap;
        this.outputValueMap = outputValueMap;
    }

    public Object getValue(String path) {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        Object obj = dataModelMap.get(path.toUpperCase());
        if (obj == null) {
            return null;
        }
        if (obj instanceof Date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) obj);
            return calendar;
        }
        return obj;
    }

    public Integer getValueAsInt(String path) {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        String p = path.toUpperCase();
        Object obj = dataModelMap.get(p);
        if (obj == null) {
            return null;
        }
        if (obj instanceof Integer ) {
            return (Integer) obj;
        } else if (obj instanceof Long){
        	long v=(Long)obj;
        	return (int)v;
        }else if (obj instanceof String) {
            String strObj = ((String) obj).toUpperCase();
            if ("Y".equals(strObj) || "1".equals(strObj)) {
                return Integer.valueOf(1);
            } else if ("N".equals(strObj) || "0".equals(strObj)) {
                return Integer.valueOf(0);
            }
        }
        return null;
    }

    public Double getValueAsDouble(String path) {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        Object obj = dataModelMap.get(path.toUpperCase());
        if (obj == null) {
            return null;
        }
        if (obj instanceof Double) {
            return (Double) obj;
        }
        return null;
    }

    public Object getTempValue(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return tempValueMap.get(key);
    }

    public void putTempValue(String key, Object value) {
        tempValueMap.put(key, value);
    }

    public void putOutputValue(String key, Object value) {
        if (StringUtils.isEmpty(key)) {
            return;
        }
        outputValueMap.put(key.toUpperCase(), value);
    }

    public Object getOutputValue(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return outputValueMap.get(key.toUpperCase());
    }

    public Map<String, Object> getTempValueMap() {
        return tempValueMap;
    }

    public Map<String, Object> getOutputValueMap() {
        return outputValueMap;
    }

    public Map<String, Object> getDataModelMap() {
        return dataModelMap;
    }

    public void setDataModelMap(Map<String, Object> dataModelMap) {
        this.dataModelMap = dataModelMap;
    }

    public void setCustomerID(String customerID) {
        this.customerID = customerID;
    }

    public String getCustomerID() {
        return customerID;
    }
}
